package io.renren.modules.archive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.StringUtils;
import io.renren.modules.archive.dao.MeetingtypeDao;
import io.renren.modules.archive.dao.NdJgMenuDao;
import io.renren.modules.archive.entity.MeetingtypeEntity;
import io.renren.modules.archive.entity.NdJgMenuEntity;
import io.renren.modules.archive.service.MeetingtypeService;
import io.renren.modules.archive.service.NdJgMenuService;
import io.renren.modules.sys.entity.SysRoleMenuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("ndJgMenuService")
public class NdJgMenuServiceImpl  extends ServiceImpl<NdJgMenuDao, NdJgMenuEntity> implements NdJgMenuService {
   @Autowired
   private NdJgMenuDao ndJgMenuDao;


    public List<Long> getNdJgLevelMenuList(Map<String, Object> params){

        List<NdJgMenuEntity> list = ndJgMenuDao.selectByNdAndJgWithLevelCondition(params);
        //只要menuId一个属性
        List<Long> ndJgMenuList = list.stream() // 创建流
                .map(NdJgMenuEntity::getMenuId) // 使用方法引用映射到menuid
                .collect(Collectors.toList());

       return ndJgMenuList;


    }

    public List<Long> getNdJgMenuList(Map<String, Object> params){
        String nd= (String) params.get("nd");
        String jg= (String) params.get("jg");
        LambdaQueryWrapper<NdJgMenuEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(nd),NdJgMenuEntity::getNd, nd)
                .eq(StringUtils.isNotBlank(jg),NdJgMenuEntity::getJg, jg)
                .select(NdJgMenuEntity::getMenuId)
        ;

        List<NdJgMenuEntity> list = ndJgMenuDao.selectList(lambdaQuery);

        //只要menuId一个属性
        List<Long> ndJgMenuList = list.stream() // 创建流
                .map(NdJgMenuEntity::getMenuId) // 使用方法引用映射到menuid
                .collect(Collectors.toList());

        return ndJgMenuList;


    }


    @Transactional
    public void updateNdJgMenu(NdJgMenuEntity ndJgMenuEntity){
        Map<String,String> map=new HashMap<>();
        map.put("nd",ndJgMenuEntity.getNd());
        map.put("jg",ndJgMenuEntity.getJg());
        map.put("level","4");
        ndJgMenuDao.deleteByLevelAndNdAndJg(map);
        ndJgMenuDao.deleteNegativeNumber();
        List<Long> menuIdList=ndJgMenuEntity.getMenuIdList();
        if(menuIdList.size() == 0){
            return ;
        }

        //保存角色与菜单关系
        for(Long menuId : menuIdList){
            NdJgMenuEntity ndJgMenu = new NdJgMenuEntity();
            ndJgMenu.setNd(ndJgMenuEntity.getNd());
            ndJgMenu.setJg(ndJgMenuEntity.getJg());
            ndJgMenu.setMenuId(menuId);

            this.save(ndJgMenu);
        }

    }

}
